package org.fei.ClassDiagram;

import org.fei.ClassDiagram.RunAction;
import org.fei.ClassDiagram.WindowComponents.MessageJPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.filesystems.FileObject;
import org.openide.util.NbBundle.Messages;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
import org.fei.ClassDiagram.ProjectScanner;
import org.openide.awt.NotificationDisplayer.Priority;
import org.openide.util.RequestProcessor;

/**
 * Trieda prida tlacitko Generate class diagram do panulu nastrojov (taka siva baseballova lopticka)
 * 
 * Tlacitko sa pokusi vygenerovat diagramy z projektu oznaceneho ako hlavny.
 * Ak projekt nie je podporovana Java SE aplikacia, vypise o tom informacnu spravu a diagram nevygeneruje.
 * @author Tomas
 */
@ActionID(category = "Build",
id = "org.fei.ClassDiagram.GenerateDiagramAction")
@ActionRegistration(iconBase = "org/fei/ClassDiagram/ball.png",
displayName = "#CTL_GenerateDiagramAction")
@ActionReferences({
    @ActionReference(path = "Toolbars/ClassDiagramPlugin", position = 0)
})
@Messages("CTL_GenerateDiagramAction=Generate Class Diagram")
public final class GenerateDiagramAction extends AbstractAction implements ActionListener {

    // background thread pool
    private static final RequestProcessor RP = new RequestProcessor(GenerateDiagramAction.class);

    @Override
    public void actionPerformed(ActionEvent e) {
        // inicializacia referencie na otvorene projekty v IDE
        OpenProjects open = OpenProjects.getDefault();
        // ziskanie referencie na hlavny projekt
        Project mainProject = open.getMainProject();
        
        if (mainProject == null) {
            String[] messages = {"Main Project not found"};
            new ClassDiagramView(new MessageJPanel(messages), "Error message");
        }
        
        ProjectScannerFactory.createProjectScanner(new ProjectScanner(mainProject));
        
        ProjectScanner ps = ProjectScannerFactory.getProjectScanner();
        
        if (ps.isProjectValid()) {
            setEnabled(false);
            RP.post(new RunAction(this, mainProject), 0, Thread.NORM_PRIORITY);

        }
        else {
            String[] messages = {"Your Main Project " + ps.getProjectName() + " is not supported project type.",
                                 "In this plugin version are supported only Java SE projects."};
            new ClassDiagramView(new MessageJPanel(messages), ps.getProjectName());
        }
    }
}
